<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>basics_add</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <style>
        .view-form .layui-input-block {
            line-height: 36px;
            background: #eeeeee;
            padding-left: 20px;
            font-size: 16px;
        }
    </style>
</head>

<body>
<div style="padding: 20px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">选择类型：</label>
            <div class="layui-input-block">
                <select name="TYPE" lay-verify="required" lay-search="">
                    <option value="">直接选择或搜索选择</option>
                    <option value="PRO_007,所属行业">所属行业</option>
                    <!--<option value="PRO_009,项目类型">项目类型</option>-->
                    <option value="PRO_010,退租原因">退租原因</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">名称：</label>
            <div class="layui-input-block">
                <input type="text" name="NAME" lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">排序号：</label>
            <div class="layui-input-block">
                <input type="number" name="SORT" lay-verify="required" placeholder="请输入排序号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item operate-button-group">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="dictionary">保存</button>
                <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
            </div>
        </div>
    </form>
</div>

<script type="text/javascript" src="../../../static/js/common/queryUtils.js"></script>
<script type="text/javascript" src="../../../static/layer/layui.js"></script>
<script>
    layui.use(['jquery', 'form', 'layer'], function(){
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;

        // 初始化下拉框
        $.ajax({
            url: "/dictionary/listPname",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.errcode === "0"){
                    var data = res.data;
                    if (!!data) {
                        for (var i = 0; i < data.length; i++) {
                            var str = '<option value="' + data[i].CODE + ',' + data[i].PNAME + '">' + data[i].PNAME + '</option>\n';
                            if (data[i].CODE != "PRO_007" && data[i].CODE != "PRO_009" && data[i].CODE != "PRO_010") {
                                $("select[name='TYPE']").append(str);
                            }
                        }
                        form.render("select");
                    }

                } else if (res.errcode === "1"){
                    layer.msg(res.errmsg, function(){});
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });

        //操作类型：add新增，edit修改，view查看
        var operateType = QueryUtils.GetQueryString("type");
        var id = QueryUtils.GetQueryString("id");
        switch (operateType) {
            case "add":
                break;
            case "edit":
                $.ajax({
                    url: "/dictionary/info",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0"){
                            $("select[name='TYPE']").val(res.data.CODE + "," + res.data.PNAME);
                            $("select[name='TYPE']").attr("disabled", "true");
                            $("input[name='NAME']").val(res.data.NAME);
                            $("input[name='SORT']").val(res.data.SORT);
                            form.render();
                        } else if (res.errcode === "1"){
                            layer.msg(res.errmsg, function(){});
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
            case "view":
                $.ajax({
                    url: "/dictionary/info",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0"){
                            $(".layui-form").addClass("view-form");
                            $("select[name='TYPE']").val(res.data.CODE + "," + res.data.PNAME);
                            $("input[name='NAME']").val(res.data.NAME);
                            $("input[name='SORT']").val(res.data.SORT);
                            form.render();
                            $(".operate-button-group").remove();
                        } else if (res.errcode === "1"){
                            layer.msg(res.errmsg, function(){});
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
        }


        //监听提交
        form.on('submit(dictionary)', function(data){
            /*layer.alert(JSON.stringify(data.field.TYPE.split(",")[0]), {
                title: '最终的提交信息'
            });*/
            if (operateType === "add") {
                layer.msg('确认保存数据吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/dictionary/save",
                            data: {CODE: data.field.TYPE.split(",")[0], PNAME: data.field.TYPE.split(",")[1], NAME: data.field.NAME, SORT: data.field.SORT},
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0"){
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭新建数据页面
                                    parent.reload();    // 父页面表格刷新
                                    parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                } else if (res.errcode === "1"){
                                    layer.msg(res.errmsg, function(){});
                                }
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            } else if (operateType === "edit") {
                layer.msg('确认保存数据吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/dictionary/edit",
                            data: {ID: id, CODE: data.field.TYPE.split(",")[0], PNAME: data.field.TYPE.split(",")[1], NAME: data.field.NAME, SORT: data.field.SORT},
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0"){
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭修改商圈页面
                                    parent.reload();    // 父页面表格刷新
                                    parent.layer.msg("修改成功", {offset: '150px'});   // 父页面弹出修改成功
                                } else if (res.errcode === "1"){
                                    layer.msg(res.errmsg, function(){});
                                }
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            }
            return false;
        });
    });
</script>
</body>

</html>